<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button id="btn">按钮</button>
</body>
<script>
//解决count被充值方法1-定义全局的,缺点,全局污染
// let count = 0

/*
document.getElementById('btn').onclick = function(){
    //每次点击函数,都会重新执行方法,count = 0
    // let count = 0
    count++
    console.log(count)
}
*/
//立即执行函数
document.getElementById('btn').onclick = (function(){
    //闭包
    let count = 0
    // 高阶函数:一个函数当中返回另外一个函数
    return function(){
        count++
        console.log(count)
    }
})() 
</script>
</html>